package utils.handlers;

import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.style.column.AbstractHeadColumnWidthStyleStrategy;

import utils.WishColumn;

public class CustomHeadColumnWidthStyleStrategy extends AbstractHeadColumnWidthStyleStrategy
{

	private List<WishColumn<?>> leafs = new ArrayList<>();

	public CustomHeadColumnWidthStyleStrategy(List<WishColumn<?>> leafs)
	{
		super();
		this.leafs = leafs;
	}

	protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead)
	{
		if (!isHead)
		{
			return;
		}
		int width = columnWidth(head, cell.getColumnIndex());
		if (width > 0)
		{
			writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), width * 256);
		}
	}

	@Override
	protected Integer columnWidth(Head head, Integer columnIndex)
	{
		try
		{
			WishColumn<?> tableHeader = leafs.get(columnIndex);
			return tableHeader.getWidth();
		}
		catch (Exception e)
		{
		}
		return 0;
	}

}